package com.zwy.Shopping.dao.impl;

import com.zwy.Shopping.connection.DBCon;
import com.zwy.Shopping.dao.OrderItemDao;
import com.zwy.Shopping.model.OrderItem;
import jdk.internal.org.objectweb.asm.tree.FieldInsnNode;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderItemDaoImpl implements OrderItemDao {

    @Override
    public List<OrderItem> getAllOrderItem() {
        Connection con = DBCon.getConnection();
        if (con == null) {
            System.out.println("数据库连接失败");
            return null;
        }

        try {
            Statement stmt = con.createStatement();
            String sql = "select * from order_item";
            ResultSet rs = stmt.executeQuery(sql);
            List<OrderItem> orderItems = new ArrayList<>();
            while (rs.next()) {
                OrderItem orderItem = new OrderItem();
                orderItem.setOrderId(rs.getInt("order_id"));
                orderItem.setId(rs.getInt("id"));
                orderItem.setPrice(rs.getBigDecimal("price"));
                orderItem.setProduct(new ProductDaoImpl().getProductById(rs.getInt("product_id")));
                orderItem.setQuantity(rs.getInt("quantity"));
                orderItems.add(orderItem);
            }

            return orderItems;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<OrderItem> getOrderItemByOrderId(int orderId) {
        Connection con = DBCon.getConnection();
        if (con == null) {
            System.out.println("数据库连接失败");
            return null;
        }
        try {
            PreparedStatement pst = con.prepareStatement("select * from order_item where order_id = ?");
            pst.setInt(1, orderId);

            ResultSet rs = pst.executeQuery();
            List<OrderItem> orderItems = new ArrayList<>();
            while (rs.next()) {
                OrderItem orderItem = new OrderItem();
                orderItem.setOrderId(rs.getInt("order_id"));
                orderItem.setId(rs.getInt("id"));
                orderItem.setPrice(rs.getBigDecimal("price"));
                orderItem.setProduct(new ProductDaoImpl().getProductById(rs.getInt("product_id")));
                orderItem.setQuantity(rs.getInt("quantity"));
                orderItems.add(orderItem);
            }


            return orderItems;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public boolean generateOrderItem(int orderId, int product_id, BigDecimal price, int quantity) {
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        con = DBCon.getConnection();
        if(con==null){
            System.out.println("数据库连接失败");
        }
        try {
            pst = con.prepareStatement("insert into order_Item(order_id,product_id,price,quantity)values(?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
            pst.setInt(1,orderId);
            pst.setInt(2,product_id);
            pst.setBigDecimal(3,price);
            pst.setInt(4,quantity);
            int affectRow = pst.executeUpdate();
            if(affectRow >0 ){
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();//食用日志记录器记录错误
            throw new RuntimeException("数据库操作失败" + e.getMessage());
        }finally {
            try {
                if(rs != null)rs.close();
                if (pst != null) pst.close();

            }catch (SQLException e){
                e.printStackTrace();//记录关闭资源时的异常
            }
        }
        return false;
    }
}
